package com.pro.common.login.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.pro.common.api.enums.EnumSysRole;
import com.pro.common.login.service.BaseLoginInfoService;
import com.pro.common.api.model.ILoginInfo;
import com.pro.common.api.sitecontent.intf.AuthRoleFeignClient;
import com.pro.common.api.sitecontent.model.request.AuthRoleGetRouteCodesRequest;
import com.pro.common.jwt.AccessTokenJwt;
import com.pro.common.api.model.request.ChangePasswordRequest;
import com.pro.common.login.service.CommonLoginService;
import com.pro.framework.api.util.AssertUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Tag(name = "统一登录注册接口")
@RestController
@RequestMapping("/commonLoginInfo")
public class CommonLoginInfoController {

    @Autowired
    private CommonLoginService commonLoginService;
    @Autowired
    private BaseLoginInfoService loginInfoService;
    @Autowired
    private AuthRoleFeignClient authRoleFeignClient;


    @ApiOperationSupport(order = 3)
    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public void logout() {
//        Long id = loginInfo.getId();
//        if (id != null) {
//            // 移除token
//            tokenService.evict(request.getToken());
//        }
        // jwt无状态,只能前端退出了
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "查询登录信息")
    @GetMapping("/getLoginInfo")
    public ILoginInfo getLoginInfo(@Parameter(hidden = true) ILoginInfo loginInfo) {
        return commonLoginService.getLoginInfo(loginInfo.getId());
    }


    @ApiOperationSupport(order = 7)
    @Operation(summary = "更新token")
    @PostMapping("/refreshToken")
    public AccessTokenJwt refreshToken(@Parameter(hidden = true) ILoginInfo loginInfo) {
        AssertUtil.isTrue(!EnumSysRole.ANONYMOUS.equals(loginInfo.getSysRole()), "暂无权限");
        // 返回token
        return commonLoginService.generateToken(loginInfo);
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "获取权限")
    @GetMapping("/getAuthRouteCodes")
    public Set<String> getAuthRouteCodes(@Parameter(hidden = true) ILoginInfo loginInfo) {
        AssertUtil.isTrue(loginInfoService.getSysRole().equals(loginInfo.getSysRole()), "暂无权限");
        return authRoleFeignClient.getRouteCodes(new AuthRoleGetRouteCodesRequest(loginInfo.getSysRole(), loginInfoService.getRoleIds(loginInfo.getId())));
    }


    @ApiOperationSupport(order = 6)
    @Operation(summary = "修改密码")
    @PostMapping("/checkAndChangePassword")
    @SneakyThrows
    public void checkAndChangePassword(@Parameter(hidden = true) ILoginInfo loginInfo, @RequestBody ChangePasswordRequest request) {
        loginInfoService.checkAndChangePassword(loginInfo, request.getPasswordOld(), request.getPassword());
    }


    @ApiOperationSupport(order = 6)
    @Operation(summary = "修改其他不重要信息")
    @PostMapping("/update")
    @SneakyThrows
    public void update(@Parameter(hidden = true) ILoginInfo loginInfo, @RequestBody String requestBody) {
        loginInfoService.update(loginInfo, requestBody);
    }
}
